
package com.netease.xone.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.widget.FrameLayout;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.fragment.FragmentInfoDetail;

public class ActivityInfoDetail extends ActivityX1Base {
    public static final int MODE_INVALID = -1;
    public static final int MODE_FROM_TOUTIAO = 0;
    public static final int MODE_FROM_INFO = 1;
    
    public static final String TAG = ActivityInfoDetail.class.getSimpleName();

    public static void startActivity(Context context, String infoId, int mode) {
        Intent intent = new Intent(context, ActivityInfoDetail.class);
        if(!(context instanceof Activity))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(XoneConsts.BUNDLE_KEY.INFO_ID, infoId);
        intent.putExtra(XoneConsts.BUNDLE_KEY.TOUTIAO_MODE, mode);
        context.startActivity(intent);
    }

    private int mMode;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mMode = MODE_INVALID;
        if (null != getIntent()) {
            mMode = getIntent().getIntExtra(XoneConsts.BUNDLE_KEY.TOUTIAO_MODE, MODE_INVALID);
        }
        
        String infoId = getIntent().getStringExtra(XoneConsts.BUNDLE_KEY.INFO_ID);
        if (TextUtils.isEmpty(infoId)) {
            finish();
            return;
        }
        
        FrameLayout linear = new FrameLayout(this);
        linear.setId(R.id.activity_info_detail_container_id);
        setContentView(linear);
        
        if (null != findViewById(R.id.activity_info_detail_container_id) && savedInstanceState == null) {
            Fragment fragment = null;
            fragment = FragmentInfoDetail.newInstance(infoId);
            getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.activity_info_detail_container_id, fragment, TAG)
            .commit();
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMode == MODE_FROM_TOUTIAO) {
            ActivityMain.launch(this);
        }
    }
}
